package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.MessageSendUtil;
import com.finace.money.common.util.PopuUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.BankContractBean;
import com.finace.money.data.bean.BankCradInfoBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.ui.widget.MyPopupWindow;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/12/6.
 */

public class BindCardRealNameActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.ll_content)
    RelativeLayout llContent;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    //绑定银行卡
    @BindView(R.id.ll_card_null)
    LinearLayout llCardNull;
    @BindView(R.id.et_name_accept)
    EditText etNameAccept;
    @BindView(R.id.et_number_person)
    EditText etNumberPerson;
    @BindView(R.id.et_number_card_accept)
    EditText etNumberCardAccept;
    @BindView(R.id.ll_choose_bank)
    LinearLayout llChooseBank;
    @BindView(R.id.tv_bank_name_show)
    TextView tvBankNameShow;
    @BindView(R.id.button_check_commit)
    Button buttonCheckCommit;

    //银行卡信息
    @BindView(R.id.ll_card_info)
    LinearLayout llCardInfo;
    @BindView(R.id.iv_bank_icon)
    ImageView ivBankIcon;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bank_end)
    TextView tvBankEnd;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_person_head)
    TextView tvPersonHead;
    @BindView(R.id.tv_person_end)
    TextView tvPersonEnd;
    @BindView(R.id.tv_card_info_tip)
    TextView tvCardInfoTip;


    private MyPopupWindow mCommitPopupWindow;
    private ArrayList<BankContractBean> mBanks;

    private int mPosition = 0;
    private OptionsPickerView optionsPickerView;
    private String cardNumber;
    private String number;
    private String name;
    private int mBankFK;

    @Override
    public int setLayout() {
        return R.layout.activity_bind_card_real_name;
    }

    @Override
    public void init() {
        rlLeft.setOnClickListener(this);
        buttonCheckCommit.setOnClickListener(this);
        llChooseBank.setOnClickListener(this);
        tvHeadTitle.setText(UIUtil.getString(R.string.bind_card_real_name));
        initBank();
        getAcache();
        Intent intent = getIntent();
        if (null != intent) {
            mBankFK = intent.getIntExtra(Constant.BANK_CARD_INFO, 0);
            if (mBankFK == 0) {
                llCardNull.setVisibility(View.VISIBLE);
                llCardInfo.setVisibility(View.GONE);
            } else {
                llCardNull.setVisibility(View.GONE);
                llCardInfo.setVisibility(View.VISIBLE);
                BankCradInfoBean bankCradInfoBean = (BankCradInfoBean) ACache.get(UIUtil.getContext()).getAsObject(Constant.BANK_CARD_INFO);
                tvName.setText(bankCradInfoBean.getName());
                tvBankName.setText("-----------");
                tvBankEnd.setText(bankCradInfoBean.getIDCard().substring(bankCradInfoBean.getIDCard().length()-4,bankCradInfoBean.getIDCard().length()));
                tvPersonHead.setText("----");
                tvPersonEnd.setText("----");
            }
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_BANK_CHANGE);
//        intentFilter.addAction(PushReceiver.ACTION_INFO_UPDATE);
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            BankContractBean change = (BankContractBean) intent.getSerializableExtra(PushReceiver.ACTION_BANK_CHANGE);
            if(null != change ){
                if (change.isIs_last()) {
                    MessageSendUtil.QryInvestor();
                    UIUtil.showToastSafe(getString(R.string.bind_sucess));
                    onBackPressed();
                } else {
                    UIUtil.showToastSafe(change.getRsp_info().getError_message());
                }

            }


        }
    };

    private void getAcache() {

        mBanks = (ArrayList<BankContractBean>) ACache.get(UIUtil.getContext()).getAsObject(Constant.BANK_LIST);
        if(null != mBanks && mBanks.size()>0){
            ArrayList<String> mBankNames = new ArrayList<>();
            for (BankContractBean bank : mBanks) {
                mBankNames.add(bank.getBankName());
            }
            optionsPickerView.setPicker(mBankNames);
        }
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                onBackPressed();
                break;
            case R.id.button_check_commit:
                name = etNameAccept.getText().toString().trim();
                number = etNumberPerson.getText().toString().trim();
                cardNumber = etNumberCardAccept.getText().toString().trim();
                if (StringUtil.isNullString(name)) {
                    UIUtil.showToastSafe(R.string.input_name_accept);
                } else if (StringUtil.isNullString(number)) {
                    UIUtil.showToastSafe(R.string.input_number_person);
                } else if (StringUtil.isNullString(cardNumber)) {
                    UIUtil.showToastSafe(R.string.input_number_card_accept);
                } else if (StringUtil.isNullString(tvBankNameShow.getText().toString().trim())) {
                    UIUtil.showToastSafe(R.string.input_bank_name);
                } else {
                    goBindPopup();
                }
                break;
            case R.id.ll_choose_bank:
                chooseBank();
                break;
            case R.id.button_check_again:
                mCommitPopupWindow.dismiss();
                break;
            case R.id.button_sure_have:
                MessageSendUtil.ChangeInvestor(name, number, mBanks.get(mPosition).getBankID(), cardNumber);
                mCommitPopupWindow.dismiss();
                break;
        }
    }

    private void chooseBank() {

        optionsPickerView.setSelectOptions(mPosition);
        optionsPickerView.show();
    }

    private void initBank() {
        optionsPickerView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                mPosition = options1;
                tvBankNameShow.setText(mBanks.get(mPosition).getBankName());
            }
        })
                .setTitleBgColor(Color.BLACK)
                .setCancelColor(Color.WHITE)
                .setSubmitColor(Color.WHITE)
                .setContentTextSize(14)//设置滚轮文字大小
                .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                .setLineSpacingMultiplier(12f)
                .setSelectOptions(0)//默认选中项
                .setBgColor(Color.BLACK)
                .setCancelColor(Color.GRAY)
                .setSubmitColor(Color.GRAY)
                .setTextColorOut(Color.WHITE)
                .setTextColorCenter(Color.WHITE)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setBackgroundId(0x66000000) //设置外部遮罩颜色
                .build();

    }

    private void goBindPopup() {
        mCommitPopupWindow = new PopuUtil().initAtLocationPopuWrap(this,
                R.layout.popup_bind_card, llContent, 265, 115);
        Button buttonCheckAgain = mCommitPopupWindow.getContentView().findViewById(R.id.button_check_again);
        Button buttonSureHave = mCommitPopupWindow.getContentView().findViewById(R.id.button_sure_have);
        buttonCheckAgain.setOnClickListener(this);
        buttonSureHave.setOnClickListener(this);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setResult(0, null);
        finish();
    }
}
